package cemp.service;

import cemp.common.util.DateUtil;
import cemp.dto.DailyTaskDto;
import cemp.entity.DailyTask;
import cemp.entity.StockSysUser;
//import cemp.enums.MessageEnum;
import cemp.mapper.DailyTaskMapper;
import cemp.mapper.StockSysUserMapper;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@Slf4j
@Service
public class DailyTaskService {

    @Resource
    private final DailyTaskMapper dailyTaskMapper;
    @Resource
    private final WxMessageService wxMessageService;
    @Resource
    private final StockSysUserMapper userMapper;

    public List<DailyTaskDto> getTasks(){
        //只显示当天的 并且倒序排列
        LambdaQueryWrapper<DailyTask> lqw = new LambdaQueryWrapper<>();
        lqw.gt(DailyTask::getCreateDate,DateUtil.localDate2Str(LocalDate.now()));
        lqw.orderByDesc(DailyTask::getCreateDate);
        return dailyTaskMapper.selectList(lqw).stream().map(new Function<DailyTask, DailyTaskDto>() {
            @Override
            public DailyTaskDto apply(DailyTask dailyTask) {
                DailyTaskDto dto = new DailyTaskDto();
                BeanUtil.copyProperties(dailyTask,dto);
                return dto;
            }
        }).collect(Collectors.toList());
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.localDate2Str(LocalDate.now()));
    }

    public void doupdate(DailyTaskDto taskDto) {
        DailyTask task = new DailyTask();
        BeanUtil.copyProperties(taskDto,task);
        dailyTaskMapper.updateById(task);
    }

    public void doadd(DailyTaskDto taskDto) {
        DailyTask task = new DailyTask();
        BeanUtil.copyProperties(taskDto,task);
        task.setCreateDate(new Date());
        task.setCompleted(0);
        dailyTaskMapper.insert(task);
    }

    //@Async("sendMsgPool")
    public void sendMsg(Integer userId, BigDecimal score){
        StockSysUser user =  userMapper.selectById(userId);
        Map<String,Object> miniParams = new HashMap<>();
        miniParams.put("thing2", "今天的分数为:" + score);
        miniParams.put("name3", user.getUsername());
        System.out.println("begin to send");
        wxMessageService.wxSendMessage(user.getOpenid(),wxMessageService.wxGetAccessToken(),
                "2IY8bSwN-Dm4YiAvyy-2PZSSDUweHIdJDy79NJaIH9c",miniParams);
    }
}
